﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CAUNetLogin.Util.SystemLib
{
    /// <summary>
    /// 获得当前网络状态
    /// </summary>
    public class NetState
    {
        [DllImport("wininet.dll")]
        private static extern bool InternetGetConnectedState(ref int dwFlag, int dwReserved);
        private const int INTERNET_CONNECTION_MODEM = 1;
        private const int INTERNET_CONNECTION_LAN = 2;
        private const int INTERNET_CONNECTION_PROXY = 4;
        private const int INTERNET_CONNECTION_MODEM_BUSY = 8;

        /// <summary>
        /// 当前网络状态
        /// </summary>
        public  bool m_bOnline = false;
        /// <summary>
        /// 当前网络状态描述
        /// </summary>
        public string netStateMessage="This is the default netstate message";
        /// <summary>
        /// 测试网络状态
        /// </summary>
        /// <returns></returns>
        public bool GetNetState()
        {
            int flag = 0;
            m_bOnline = InternetGetConnectedState(ref flag, 0);//Wininet.dll
            if (m_bOnline)
            {
                if ((flag & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN)
                {
                    netStateMessage = "网络正常--局域网";
                }
                else if ((flag & INTERNET_CONNECTION_MODEM) == INTERNET_CONNECTION_MODEM)
                {
                    netStateMessage = "网络正常--ADSL";
                }
                else if ((flag & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY)
                {
                    netStateMessage = "网络正常--PROXY";
                }
                else if ((flag & INTERNET_CONNECTION_MODEM_BUSY) == INTERNET_CONNECTION_MODEM_BUSY)
                {
                    netStateMessage = "网络正常--MODEM_BUSY";
                }
                else
                {
                    netStateMessage = "网络正常--OTHER TYPE(ONLINE)";
                }
            }
            else
            {
                netStateMessage = "网络异常!";
                return false;
            }

            return true;
        }

        /// <summary>
        /// Ping IP
        /// </summary>
        /// <param name="ip">首ping ip，若ping不通，则使用ip2</param>
        /// <param name="ip2">若都屏不同，则失败</param>
        /// <returns></returns>
        public bool Ping(string ip,string ip2)
        {
            bool pingState = false;//标识量
            try
            {
                System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
                System.Net.NetworkInformation.PingReply pingReply = ping.Send(System.Net.IPAddress.Parse(ip), 500);//Ping ip1，500毫秒超时
                //判断ping返回来的结果
                pingState = (pingReply.Status == System.Net.NetworkInformation.IPStatus.Success);
                if (pingState)
                {
                    return true;
                }
                else
                {
                    pingReply = ping.Send(System.Net.IPAddress.Parse("74.125.71.94"), 500);
                    if (pingReply.Status == System.Net.NetworkInformation.IPStatus.Success)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

    }
}
